home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / dev / c / expat-dev.lha / expat-1.95.5 / examples / elements.c next >
C/C++ Source or Header  |  2002-09-09  |  2KB  |  80 lines

  1. /* This is simple demonstration of how to use expat. This program
  2.    reads an XML document from standard input and writes a line with
  3.    the name of each element to standard output indenting child
  4.    elements by one tab stop more than their parent element.
  5. */
  6.  
  7. #include <stdio.h>
  8. #ifndef AMIGA
  9. #include <expat.h>
  10. #else
  11. #include <exec/types.h>
  12. #include <exec/memory.h>
  13. #include <expat/expat.h>
  14. #include <proto/exec.h>
  15. #include <proto/expat.h>
  16.  
  17. struct Library *ExpatBase = NULL;
  18.  
  19. #endif
  20.  
  21. static void
  22. startElement(void *userData, const char *name, const char **atts)
  23. {
  24.   int i;
  25.   int *depthPtr = userData;
  26.   for (i = 0; i < *depthPtr; i++)
  27.     putchar('\t');
  28.   puts(name);
  29.   *depthPtr += 1;
  30. }
  31.  
  32. static void
  33. endElement(void *userData, const char *name)
  34. {
  35.   int *depthPtr = userData;
  36.   *depthPtr -= 1;
  37. }
  38.  
  39. int
  40. main(int argc, char *argv[])
  41. {
  42.   char buf[BUFSIZ];
  43.   XML_Parser parser;
  44.   int done;
  45.   int depth = 0;
  46.  
  47. #ifdef AMIGA
  48.   ExpatBase = (APTR) OpenLibrary("expat.library", 0);
  49.   if(!ExpatBase)
  50.   {
  51.     puts("\nCouldn't open expat.library\n");
  52.     exit(20);
  53.   }
  54. #endif
  55.  
  56.   parser = XML_ParserCreate(NULL);
  57.   
  58.   XML_SetUserData(parser, &depth);
  59.   XML_SetElementHandler(parser, startElement, endElement);
  60.   do {
  61.     size_t len = fread(buf, 1, sizeof(buf), stdin);
  62.     done = len < sizeof(buf);
  63.     if (XML_Parse(parser, buf, len, done) == XML_STATUS_ERROR) {
  64.       fprintf(stderr,
  65.           "%s at line %d\n",
  66.           XML_ErrorString(XML_GetErrorCode(parser)),
  67.           XML_GetCurrentLineNumber(parser));
  68. #ifdef AMIGA  
  69.       CloseLibrary((APTR) ExpatBase);;
  70. #endif
  71.       return 1;
  72.     }
  73.   } while (!done);
  74.   XML_ParserFree(parser);
  75. #ifdef AMIGA  
  76.       CloseLibrary((APTR) ExpatBase);;
  77. #endif
  78.   return 0;
  79. }
  80.